Sending e-mails

Sending an e-mail with ASP.NET is quite easy. The .NET framework comes with an entire namespace to handle e-mail, system.net.mile namespace. In the following examples, we will use two squares from this namespace: To send e-mail, mail message for actual e-mail and SmtpClient class.

As you know, mails are sent through a SMTP server, and To send mail with the NET framework you will need access to a SMTP server. If you are testing things locally, then the company that supplies with your internet access usually has an SMTP server that you can use, and if you want to do any of these examples If you want to use on your actual website, then the company hosting your website is usually an SMTP server you can use. Go through support pages to find the real address - this is usually on the lines of smtp.your-isp.com or mail.your-isp.com.

After you have an accessible SMTP server, we are ready to send your first e-mail. For the first example, you have only one blank page, in which the code follows the following code:


protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("[email protected]");
        mailMessage.From = new MailAddress("[email protected]");
        mailMessage.Subject = "ASP.NET e-mail test";
        mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
        SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
        smtpClient.Send(mailMessage);
        Response.Write("E-mail sent!");
    }
    catch(Exception ex)
    {
        Response.Write("Could not send the e-mail - error: " + ex.Message);
    }
}

It really requires you to send e-mails. We create a new mailmass example, add a new receiver, set the "sender" address and the subject, and then we have a simple test message for the body of e-mail Writes. After this, we create a new example of SMTPClient, with the host address of the SMTP server that you can use as a parameter, and then we use SmtpClient instance to shoot e-mail in cyberspace. The whole thing is a try ... surrounded by a tornado, just when something goes wrong.

It was just a very basic example, but there are many other options, with a small list of interesting ideas:

You can add one or several files by adding it to the attachment collection. In this example, we attach a file named "image.jpg" located in the root of the ASP.NET website.


mailMessage.Attachments.Add(new Attachment(Server.MapPath("~/image.jpg")));

You can send to more than one person at the same time, simply by adding another e-mail address to the "To" collection, like this:


mailMessage.To.Add("[email protected]");
mailMessage.To.Add("[email protected]");

You can set a name for the sender - otherwise, only the e-mail address will be shown in the "From" column of the receivers e-mail client. For instance, like this:


mailMessage.From = new MailAddress("[email protected]", "My Name");

You can send HTML e-mails, instead of the default plaintext mails, to use more complicated layouts. Here is a simple example:


mailMessage.IsBodyHtml = true;
mailMessage.Body = "Hello <b>world!</b>";

You can use the CC and BCC fields, just like in regular e-mail messages, like this:


mailMessage.CC.Add("[email protected]");
mailMessage.Bcc.Add("[email protected]");

You can set the priority of an e-mail, like this:


mailMessage.Priority = MailPriority.High;